package com.zsc.newbuds.controller.admin;

import com.zsc.newbuds.entity.Admin;
import com.zsc.newbuds.entity.Application;
import com.zsc.newbuds.service.AdminService;
import com.zsc.newbuds.service.impl.AdminServiceImpl;
import com.zsc.newbuds.service.impl.ApplicationServiceImpl;
import com.zsc.newbuds.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author D
 */
@RequestMapping("admin/volunteerManagement")
@Controller
public class VolunteerController {

    @Autowired
    ApplicationServiceImpl applicationService;

    @Autowired
    AdminServiceImpl adminService;

    @Autowired
    UserServiceImpl userService;

    @ResponseBody
    @GetMapping("/getVolunteerList")
    public List<Application> getVolunteerApplicationList(){
        List<Application> applicationList = applicationService.getApplicationList();
        System.out.println(applicationList);
        return applicationList;
    }

    @ResponseBody
    @GetMapping("/eliminateApplication")
    public void eliminateApplication(Integer id){
        System.out.println("申请者"+id);

        //申请失败设置状态0
        userService.addVolunteerId(id,0);
        applicationService.updateState(id,"申请失败");
    }

    @ResponseBody
    @GetMapping("/passApplication")
    public void passApplication(Integer id,String name,String phone,String address){
        System.out.println("申请者"+id+" "+name+" "+phone);
        String account  = Integer.toString(id)+ phone.substring(0,3);
        System.out.println(account);

        //创建一个后台账号
        Admin admin = new Admin();
        admin.setAccount(account);
        admin.setAdminName(name);
        admin.setPhone(phone);
        admin.setPassword("123456");
        admin.setPosition("volunteer");
        admin.setOderNumber(0);
        admin.setAddress(address);
        admin.setAdminImage("http://dd.ddandang.top/headportrait/test.jpeg");
        System.out.println(admin);
        adminService.addVolunteer(admin);
        System.out.println(admin.getAdminId());

        //更用户增加后台账号id

        userService.addVolunteerId(id,admin.getAdminId());

        applicationService.updateState(id,"申请成功");


    }
}
